<template>
	<view>
		<view name="barrage" class="barrage" :style="{bottom:bottom+'rpx',left:left+'rpx'}" >
			<view>
				<view v-for="(item,index) in barrageList" :key="item.id"  style="width: 400rpx;" class="barrage-item" ref="bar">
					<u-transition :show="show" :mode="['slide-up']" @afterEnter='afterEnter(index)' :timingFunction='timingFunction' :duration='duration'>
						<view style="padding:6rpx 0;border-radius: 30rpx;font-size: 24rpx;display: flex;align-items: center;" :animation="animationData[index]" :style="{backgroundColor:item.type==1?'#f65e66':item.type==0?'#72aaf8':item.type==2?'#f2922e':item.type==3?'#a291fd':'#eee'}">
							<image class="barrage-image" :src="imgs[item.type]"  style="width: 120rpx;height: 40rpx;margin-left: 6rpx;"></image>
							<view class="barrage-text" style="margin: 0 20rpx;width: 80rpx;white-space: nowrap;overflow: hidden;text-overflow: ellipsis;" :style="{color:color,opacity:opacity}">{{processName(item.name)}}</view>
							<text class="barrage-text" :style="{color:color,opacity:opacity}">{{item.text}}</text>
						</view>
					</u-transition>
				</view>
			</view>
		</view>
	</view>
</template>

<script>
	export default {
		name: "sinBarrage",
		props: {
			list: {
				type: Array || Object,
				default () {
					return {}
				}
			},
			rows: {
				type: Number,
				default: 3
			},
			color: {
				type: String,
				default: '#FFFFFF'
			},
			background: {
				type: String,
				default: '#000000'
			},
			opacity: {
				type: Number,
				default: 0.7
			},
			left: {
				type: Number,
				default: 35
			},
			bottom: {
				type: Number,
				default: 120
			},
			msec: {
				type: Number,
				default: 2000
			}
		},
		data() {
			return {
				barrageList: [],
				duration:'',
				timingFunction:'',
				time:{},
				update:false,
				updates:true,
				index:0,
				topnum:0,
				top:0,
				imgs:['https://cdn.yowswl.com/image/admin/2023/11/30/56ddcc56c4eb4a29b2d40e2713528395jf44rvdij2.png','https://cdn.yowswl.com/image/admin/2023/12/01/41e233a8568947738d8a5311fb58d9afgghrof4ukj.png','https://cdn.yowswl.com/image/admin/2023/12/01/3591b98646ea44e0a9279b2e6f4442be84vglnk35h.png','https://cdn.yowswl.com/image/admin/2023/12/01/610fc810baf8462e8b15a6c487ccc6966p5vkegdbe.png'],
				animationData:{},
				animation:{},
				show: true
			}
		},
		onReady(){
			 
		},
		mounted() {
			this.time =setInterval(() => {
				/** 此处逻辑：
				 * 设定A数组为展示数组（默认3条数据），B数组为源数组（n条数据）
				 * 首次进入页面，因A数组为空，所以根据rows设定每次从B数组头部取1条数据移入A数组尾部
				 * 此后，在每个定时周期内，把A数组头部第1条数据移出来，并移入B数组尾部
				 * 同时把B数组头部第1条数据移出来，并移入A数组尾部
				 * 如此循环即可
				 */
				// if(this.fags){
				// 	this.barrageList.push(this.list[0])
				// }
				// this.fags = false
				if(this.index<this.list.length-1){
					this.index++
				}else{
					this.index = 0
					
				}
				this.barrageList.push(this.list[this.index])

				console.log('leib',this.barrageList);
				
			}, this.msec)
		},
	     beforeDestroy(){
			clearInterval(this.time) 
		 },
		methods: {
			afterEnter(index){
			  if(this.barrageList.length>2){
				  for (var i = 0; i < this.barrageList.length; i++) {
				  	let a = {}
				  	a[i] = {}
				  	this.animationData = { ...this.animationData,
				  		...a
				  	}
				  }
				  var animation = uni.createAnimation({
				  	duration: 500,
				  	timingFunction: 'ease-in',
					property:'all'
				  })
				  setTimeout(()=>{
				  					  this.duration = 200
				  					  this.timingFunction = 'ease-in'
				  },200)
				  this.animation = animation
				   this.animation.translateX(-250).step()
				   this.animationData[index-2] = this.animation.export()
				  setTimeout(()=>{
					 // this.top = (index-1)*25
					 let arr = this.barrageList.slice(0,index-1)
					 this.barrageList = this.barrageList.filter(item=>!arr.some(obj=>obj.name==item.name))
					  var animation = uni.createAnimation({
					  	duration:0,
					  	timingFunction: 'ease-in',
						property:'all'
					  })
					  this.animation = animation
					  this.animation.translateX(0).step()
					  this.animationData[index-2] = this.animation.export()
				  },500)
				  
			  }
				
			},
            processName(str){
                  if(null != str && str != undefined){
                    let star = '' //存放名字中间的*
            		let name = ''
                    //名字是两位的就取姓名首位+*
                    if(str.length > 2){
                      for (var i = 0; i < str.length-2; i++) {
                        star= star + ' * '
                      }
                      name=str.substring(0,1)+ star + str.substring(str.length-1,str.length);
                    }else{
            			for (var i = 0; i < str.length-1; i++) {
            			  star= star + ' * '
            			}
            		   name=str.substring(0,1)+ star
					}
            	  return name	
                }
            },
		
			
		}
	}
</script>

<style lang="scss">
	// .barrage-item {
	// 	animation: box1 3s linear;
	// }
    
 //    @keyframes fadeIn  {
	// 		from {
	// 			opacity: 1;
	// 			transform: translate(0, 30px)
	// 		}
	
	// 		to {
	// 			opacity: 0;
	// 			transform: translate(-300px,-20px)
	// 		}
 //    }
	// @keyframes box1 {
	// 		from {
	// 			transform: translateY(30px)
	// 		}
	
	// 		to {
	// 			transform: translatey(-100px)
	// 		}
	// }
   .tex{
      
		
   }
	.barrage-enter-active {
		// position: absolute;
	}

	.barrage-leave-active {
		// transition: all .3s;
	}

	.barrage {
		position: absolute;
	    height: 200rpx;
		width:400rpx;
		overflow: hidden;
		.barrage-item {
			margin-top: 10rpx;
           
			.barrage-image {
				display: inline-block;
				vertical-align: middle;
				border-radius: 30rpx;
			}

			.barrage-text {
				font-size: 26rpx;
				
				color: #FFFFFF;
			}
		}
	}
</style>
